package com.small.nacos.config.server.service.notify;

import com.alibaba.nacos.common.http.AbstractHttpClientFactory;
import com.alibaba.nacos.common.http.HttpClientBeanHolder;
import com.alibaba.nacos.common.http.HttpClientConfig;
import com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate;
import com.small.nacos.sys.utils.PropertyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConfigHttpClientManager {

    private static final Logger LOGGER = LoggerFactory.getLogger(ConfigHttpClientManager.class);

    private static final NacosAsyncRestTemplate NACOS_ASYNC_REST_TEMPLATE;

    static {
        NACOS_ASYNC_REST_TEMPLATE = HttpClientBeanHolder.getNacosAsyncRestTemplate(
                new ConfigHttpClientFactory(PropertyUtil.getNotifyConnectTimeout(),
                        PropertyUtil.getNotifySocketTimeout()));
    }

    public static NacosAsyncRestTemplate getNacosAsyncRestTemplate() {
        return NACOS_ASYNC_REST_TEMPLATE;
    }


    private static class ConfigHttpClientFactory extends AbstractHttpClientFactory {

        private final int conTimeOutMillis;

        private final int readTimeOutMillis;

        public ConfigHttpClientFactory(int conTimeOutMillis, int readTimeOutMillis) {
            this.conTimeOutMillis = conTimeOutMillis;
            this.readTimeOutMillis = readTimeOutMillis;
        }

        @Override
        protected HttpClientConfig buildHttpClientConfig() {
            return HttpClientConfig.builder().setConTimeOutMillis(conTimeOutMillis)
                    .setReadTimeOutMillis(readTimeOutMillis).build();
        }

        @Override
        protected Logger assignLogger() {
            return LOGGER;
        }
    }
}
